using UnityEngine;
using System.Collections;

public class RandomMove : MonoBehaviour {
	
	Vector3 Direction;
	public float Speed = 1000f;
	
	float AnimTiming = 0.2f;
	float CurrentAnimTime = 0f;
	
	C2DSprite m_Sprite;
	
	// Use this for initialization
	void Start () 
	{
		m_Sprite = GetComponent<C2DSprite>();
		Direction = new Vector3( Random.Range( -1f , 1f) , Random.Range( -1f, 1f ) , 0f );
		CurrentAnimTime = 0f;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if( transform.localPosition.x > 1280 )
		{
			Direction = new Vector3( -1f , Random.Range( -1f, 1f ) , 0f );
		}
		
		if( transform.localPosition.x < 0 )
		{
			Direction = new Vector3( 1f , Random.Range( -1f, 1f ) , 0f );
		}
		
		if( transform.localPosition.y > 0 )
		{
			Direction = new Vector3( Random.Range( -1f , 1f) ,-1f , 0f );
		}
		
		if( transform.localPosition.y < -720 )
		{
			Direction = new Vector3( Random.Range( -1f , 1f) ,1f , 0f );
		}
		
		transform.localPosition += Direction * Speed * Time.deltaTime;
		
		if( CurrentAnimTime <= 0.0f )
		{
			CurrentAnimTime = AnimTiming; 
			
			m_Sprite.SetSpriteIndex( m_Sprite.GetSpiteIndex() + 1 );
		}
		else
		{
			CurrentAnimTime -= Time.deltaTime;
		}
	}
}
